package com.nhc.database.dbmodel;

import android.graphics.Color;

import com.astrob.model.LonLat;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Transient;
import org.osmdroid.util.GeoPoint;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * http://blog.csdn.net/njweiyukun/article/details/51893092
 * Created by wsf on 2017/5/16.
 */
@Entity
public class WayTrail {

    @Id(autoincrement = true)
    private Long id;
    private String name = "";
    private double trail_distance = 0;//航线距离
    private double line_distance = 0;//直线距离
    private double up = 0; //总爬升距离 m
    private double down = 0; //总下山距离 m

    private long starttime = 0;
    private long endtime = 0;//对应的航点， 由时间来统计
    private int totaltime = 0; //总时间 秒
    private int movingtime = 0; //活动时间 秒
    private int stoptime = 0; //停止时间 秒
    private float averagevel = 0; //平均移动速度
    private int area = 0;   //平方米
    private int movingdistance = 0;//活动距离
    private int num = 0; //记录的点数
    private int minalt = 0;//最低高度
    private int maxalt = 0;//最高高度
    private int calorie = 0;//卡路里 kcal kg*hour*K  (1小时8公里 K=0.13)

    private int colorstyle = 1; //航迹颜色样式
    private int pathstyle = 0; //航迹样式
    private boolean isshow = true; //当前显示，close后则不显示

    private String note = "";//备用 json

    @Transient
    private boolean isPause = false;//是否暂停
    @Transient
    private double distance2pt = 0;//航迹平均两点的距离，for 判断屏幕外扩的范围，过滤屏幕内的航迹，还是用Max经纬度差
    //|AB| = √[(x₁-x₂)²+(y₁-y₂)²] <  |x₁-x₂|+|y₁-y₂|

    @Transient
    public int interval = 0;//航迹平均间隔xx秒，

    @Transient
    public boolean isReverse = false;//是否返程导航

    @Transient
    public String strDate = "";//日期标签

    @Transient
    public ArrayList<TrackPoint> trackPointList = null;//画图时即时查询数据库
    //    "黑色", "红色", "蓝色", "绿色", "黄色" Color.parseColor("#AAE87A22")

    @Transient
    List<TrackPoint> points_3 = new ArrayList<>(); //绘图时高层显示数据保存 glen==3  --- 原始坐标

    @Transient
    List<TrackPoint> points_4 = new ArrayList<>(); //绘图时高层显示数据保存 glen==4

    public List<GeoPoint> getGPlist(int glen, boolean isgcj) {

        List<GeoPoint> points = new ArrayList<>();
        if (trackPointList == null || trackPointList.size() < 1) {
            points_3.clear();
            points_4.clear();
            return points;
        }

        if (trackPointList.size() != num) {
            points_3.clear();
            points_4.clear();
            num = trackPointList.size();
        }

        List<TrackPoint> tpList = new ArrayList<>();
        if (glen == 3 && points_3.size() > 1) {
            tpList = points_3;
        } else if (glen == 4 && points_4.size() > 1) {
            tpList = points_4;
        } else {
            String strGeohashLast = null;
            int size = trackPointList.size();
            for (int i = 0; i < size; i++) {
                TrackPoint tp = trackPointList.get(i);
                String strGeohash = tp.getGeohash(glen);
                if (strGeohashLast != null && strGeohash.equalsIgnoreCase(strGeohashLast)) {
                    continue;
                }
                strGeohashLast = strGeohash;
                tpList.add(tp);
            }

            if (glen == 3) {
                points_3 = tpList;
            } else if (glen == 4) {
                points_4 = tpList;
            }
        }

        if (isReverse) {
            List<TrackPoint> tpListR = new ArrayList<>();
            for (int i = 0; i < tpList.size(); i++) {
                tpListR.add(0, tpList.get(i));
            }
            tpList = tpListR;
        }

        for (int i = 0; i < tpList.size(); i++) {
            TrackPoint tp = tpList.get(i);
            LonLat poiLL = tp.getLL();
            if (isgcj) {
                poiLL = tp.getLL_gcj();
            }
            points.add(new GeoPoint(poiLL.GetLat(), poiLL.GetLon()));
        }

        return points;
    }

    public void checkGeohash() {
        int size = trackPointList.size();
        for (int i = 0; i < size; i++) {
            trackPointList.get(i).getGeohash(10);
        }
    }

    @Transient
    private static final String[] COLORS = {"#AA181818", "#AADD3833", "#AA47B1F9", "#AA868755", "#AAF4AA2B"};
//    "实线", "虚线"

    @Generated(hash = 414334067)
    public WayTrail(Long id, String name, double trail_distance, double line_distance, double up, double down,
            long starttime, long endtime, int totaltime, int movingtime, int stoptime, float averagevel, int area,
            int movingdistance, int num, int minalt, int maxalt, int calorie, int colorstyle, int pathstyle, boolean isshow,
            String note) {
        this.id = id;
        this.name = name;
        this.trail_distance = trail_distance;
        this.line_distance = line_distance;
        this.up = up;
        this.down = down;
        this.starttime = starttime;
        this.endtime = endtime;
        this.totaltime = totaltime;
        this.movingtime = movingtime;
        this.stoptime = stoptime;
        this.averagevel = averagevel;
        this.area = area;
        this.movingdistance = movingdistance;
        this.num = num;
        this.minalt = minalt;
        this.maxalt = maxalt;
        this.calorie = calorie;
        this.colorstyle = colorstyle;
        this.pathstyle = pathstyle;
        this.isshow = isshow;
        this.note = note;
    }

    @Generated(hash = 1254788365)
    public WayTrail() {
    }

    public WayTrail(String name) {
        this.id = null;
        this.name = name;
        this.starttime = System.currentTimeMillis();
        endtime = System.currentTimeMillis();
        randColor();
    }

    public void clone(WayTrail wt) {
        if (wt == null) {
            return;
        }

//        trackPointList = (ArrayList) wt.trackPointList.clone();//clone无效
        trackPointList = new ArrayList<TrackPoint>();
        for (int i = 0; i < wt.trackPointList.size(); i++) {
            TrackPoint tp0 = wt.trackPointList.get(i);
            TrackPoint tp = new TrackPoint(null, tp0.getLongitude(), tp0.getLatitude(), tp0.getAltitude(),
                    tp0.getSpeed(), tp0.getDir(), tp0.getAccuracy(), tp0.getDatetime(), 0L, "", "");
            trackPointList.add(tp);
        }

        this.name = wt.name;
        this.trail_distance = wt.trail_distance;
        this.line_distance = wt.line_distance;
        this.up = wt.up;
        this.down = wt.down;
        this.starttime = wt.starttime;
        endtime = System.currentTimeMillis();
//        this.endtime = wt.endtime;
//        this.totaltime = wt.totaltime;
        this.totaltime = (int)(endtime - wt.starttime) / 1000;
        this.movingtime = wt.movingtime;
        this.stoptime = wt.stoptime;
        this.averagevel = wt.averagevel;
        this.area = wt.area;
        this.movingdistance = wt.movingdistance;
        this.num = wt.num;
        this.minalt = wt.minalt;
        this.maxalt = wt.maxalt;
        this.calorie = wt.calorie;
        this.colorstyle = wt.colorstyle;
        this.pathstyle = wt.pathstyle;
        this.isshow = wt.isshow;
        this.isReverse = wt.isReverse;
        this.colorstyle = 1;
    }

    public void randColor() {
        //随机颜色
        Random rand = new Random();
//        int i = rand.nextInt(100);
//        生成0-100之间的随机数，包括0，不包括100。
        this.colorstyle = rand.nextInt(COLORS.length);
    }

    public void reset() {
        trail_distance = 0;//航线距离
        line_distance = 0;//直线距离
        up = 0; //总爬升距离 m
        down = 0; //总下山距离 m

        this.starttime = System.currentTimeMillis();
        endtime = System.currentTimeMillis();//对应的航点， 由时间来统计
        totaltime = 0; //总时间 秒
        movingtime = 0; //活动时间 秒
        stoptime = 0; //停止时间 秒
        averagevel = 0; //平均移动速度
        area = 0;   //平方米
        movingdistance = 0;//活动距离
        num = 0; //记录的点数
        minalt = 0;//最低高度
        maxalt = 0;//最高高度
        isshow = true;

        if (trackPointList != null) {
            trackPointList.clear();
        }
    }

    public int getColor() {
        if (colorstyle < 0 || colorstyle >= COLORS.length) {
            colorstyle = 0;
        }
        return Color.parseColor(COLORS[colorstyle]);
    }

    public boolean getIsPause() {
        return this.isPause;
    }

    public void setIsPause(boolean isPause) {
        this.isPause = isPause;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getTrail_distance() {
        return this.trail_distance;
    }

    public void setTrail_distance(double trail_distance) {
        this.trail_distance = trail_distance;
    }

    public double getLine_distance() {
        return this.line_distance;
    }

    public void setLine_distance(double line_distance) {
        this.line_distance = line_distance;
    }

    public double getUp() {
        return this.up;
    }

    public void setUp(double up) {
        this.up = up;
    }

    public double getDown() {
        return this.down;
    }

    public void setDown(double down) {
        this.down = down;
    }

    public long getStarttime() {
        return this.starttime;
    }

    public void setStarttime(long starttime) {
        this.starttime = starttime;
    }

    public long getEndtime() {
        return this.endtime;
    }

    public void setEndtime(long endtime) {
        if (!isPause) {
            this.endtime = endtime;
        }
    }

    public int getTotaltime() {
        return this.totaltime;
    }

    public void setTotaltime(int totaltime) {
        this.totaltime = totaltime;
    }

    public int getMovingtime() {
        return this.movingtime;
    }

    public void setMovingtime(int movingtime) {
        this.movingtime = movingtime;
    }

    public int getStoptime() {
        return this.stoptime;
    }

    public void setStoptime(int stoptime) {
        this.stoptime = stoptime;
    }

    public float getAveragevel() {
        return this.averagevel;
    }

    public void setAveragevel(float averagevel) {
        this.averagevel = averagevel;
    }

    public int getArea() {
        return this.area;
    }

    public void setArea(int area) {
        this.area = area;
    }

    public int getMovingdistance() {
        return this.movingdistance;
    }

    public void setMovingdistance(int movingdistance) {
        this.movingdistance = movingdistance;
    }

    public int getNum() {
        return this.num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getMinalt() {
        return this.minalt;
    }

    public void setMinalt(int minalt) {
        this.minalt = minalt;
    }

    public int getMaxalt() {
        return this.maxalt;
    }

    public void setMaxalt(int maxalt) {
        this.maxalt = maxalt;
    }

    public int getCalorie() {
        return this.calorie;
    }

    public void setCalorie(int calorie) {
        this.calorie = calorie;
    }

    public int getColorstyle() {
        return this.colorstyle;
    }

    public void setColorstyle(int colorstyle) {
        this.colorstyle = colorstyle;
    }

    public int getPathstyle() {
        return this.pathstyle;
    }

    public void setPathstyle(int pathstyle) {
        this.pathstyle = pathstyle;
    }

    public boolean getIsshow() {
        return this.isshow;
    }

    public void setIsshow(boolean isshow) {
        this.isshow = isshow;
    }

    public String getNote() {
        return this.note;
    }

    public void setNote(String note) {
        this.note = note;
    }


}
